/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.admin.flow.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fuyao.cloud.admin.flow.dto.ParamVo;
import com.fuyao.cloud.admin.flow.model.ModelInfo;
import com.fuyao.cloud.admin.flow.model.PagerModel;
import com.fuyao.cloud.admin.flow.service.flowable.IModelInfoService;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.common.security.service.CloudUser;
import com.fuyao.cloud.common.security.util.SecurityUtils;
import com.fuyao.cloud.flowable.vo.ReturnCode;
import com.fuyao.cloud.flowable.vo.ReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/model-info" )
@Api(value = "流程信息管理", tags = "流程信息管理")
public class ModelInfoController {
    private final IModelInfoService modelInfoService;

    @ApiOperation(value = "获取流程模型分页")
    @ApiImplicitParam(name = "params", value = "分页查询参数", required = true, dataTypeClass = ParamVo.class, paramType = "body")
    @PostMapping(value = "/getPage")
    public R<PagerModel<ModelInfo>> getPage(@RequestBody ParamVo<ModelInfo> params) {
        return R.ok(modelInfoService.getPagerModel(params.getEntity(), params.getQuery()));
    }


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) String id) {
        return R.ok(modelInfoService.getById(id));
    }

    /**
     * 新增
     * @param modelInfo
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    public R save(@RequestBody ModelInfo modelInfo) {
        return R.ok(modelInfoService.save(modelInfo));
    }

    /**
     * 修改
     * @param modelInfo
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    public R updateById(@RequestBody ModelInfo modelInfo) {
        return R.ok(modelInfoService.updateById(modelInfo));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable String id) {
        return R.ok(modelInfoService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param modelInfo 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<ModelInfo> export(ModelInfo modelInfo) {
        return modelInfoService.list(Wrappers.query(modelInfo));
    }

    /**
     * 保存模型
     *
     * @param modelInfo 参数
     * @return
     */
    @ApiOperation(value = "保存或修改模型对象", notes = "保存或修改模型对象")
    @PostMapping(value = "/saveOrUpdateModelInfo", produces = "application/json")
    public R<ModelInfo> saveOrUpdateModelInfo(@RequestBody ModelInfo modelInfo) {
        if (StringUtils.isBlank(modelInfo.getId())) {
            ModelInfo mi = modelInfoService.getModelInfoByModelKey(modelInfo.getModelKey());
            if (mi != null) {
                return R.failed("流程编码不能重复！");
            }
        }
       CloudUser user = SecurityUtils.getUser();
       modelInfo = modelInfoService.saveOrUpdateModelInfo(modelInfo, user, true);
        return R.ok(modelInfo);
    }

    @ApiOperation(value = "判断流程编码modelKey是否存在", notes = "判断流程编码modelKey是否存在")
    @PostMapping(value = "/checkEntityExist/{modelKey}", produces = "application/json")
    public R<Boolean> checkEntityExist(@PathVariable("modelKey") String modelKey) {
        return R.ok(modelInfoService.checkEntityExist(modelKey));
    }
}
